#ifndef _TCPCONNECTION_H
#define _TCPCONNECTION_H
#include "Socket.hpp"
#include "SocketIO.hpp"
#include "InetAddress.hpp"
#include "Packet.hpp"
#include <functional>
#include <memory>
#include <iostream>
using std::cout;
using std::endl;
using std::function;
using std::shared_ptr;
class TcpConnection;
using TcpConnectionPtr = shared_ptr<TcpConnection>;
using TcpConnectionCall = function<void(TcpConnectionPtr)>;

class EventLoop;

class TcpConnection 
:public std::enable_shared_from_this<TcpConnection>
{
public:
    TcpConnection(int fd,EventLoop * loop);
    ~TcpConnection(){
        cout<<"~TcpConnection"<<endl;
    }
    void setFunctionsCall(const TcpConnectionCall& onConnection
                          ,const TcpConnectionCall& onMessage
                          ,const TcpConnectionCall& onclose){

        _onConnection = onConnection;
        _onMessage = onMessage;
        _onclose = onclose;

    }
    int fd(){
        return _sock.fd();
    }
    bool isClosed();
    string recieve();
    Packet recvPacket();
    void send(const string & msg);
    void sendInLoop(const string & msg);
    string toString()const ;
    void shutdown();

    void handleNewConnectCallBack();
    void handleMessageCallBack();
    void handleCloseCallBack();

private:
    InetAddress getLocalAddr();
    InetAddress getPeerAddr();
private:
    Socket _sock;
    SocketIO _sockIO;
    InetAddress _local;
    InetAddress _peer;

    TcpConnectionCall _onConnection;
    TcpConnectionCall _onMessage;
    TcpConnectionCall _onclose;

    EventLoop * _loop;
};

#endif 



